/**
 * Copyright 2021 xpstem.com
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.xpstem.lelink;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;

import com.xpstem.lelink.connection.BrickConnection;
import com.xpstem.lelink.component.VerticalSeekBar;
import com.xpstem.lelink.device.EV3InputDevice;
import com.xpstem.lelink.device.EV3OutputDevice;
import com.xpstem.lelink.device.EV3Sensor;
import com.xpstem.lelink.enums.InputPort;
import com.xpstem.lelink.enums.Layer;
import com.xpstem.lelink.enums.OutputPort;
import com.xpstem.lelink.component.SpeedBoardView;
import com.xpstem.lelink.component.SteeringWheelView;

import java.io.IOException;

/**
 * @author billy zhang
 */
public class CarActivity extends AppCompatActivity {

    public static final String TAG = CarActivity.class.getSimpleName();

    private SteeringWheelView steeringWheelView;
    private SpeedBoardView speedBoardView;

    private BrickConnection brickConnection;
    private EV3BrickClient brickClient;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_car);

        speedBoardView = (SpeedBoardView)findViewById(R.id.speedBoardView);
        steeringWheelView = (SteeringWheelView)findViewById(R.id.steeringWheelView);

        // 档位
        VerticalSeekBar gearSeekBar = (VerticalSeekBar)findViewById(R.id.gearSeekBar);

        // 油门
        Button accelBtn = (Button)findViewById(R.id.accelBtn);
        accelBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //
            }
        });

        // 刹车
        Button brakeBtn = (Button)findViewById(R.id.stopBtn);
        brakeBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //
            }
        });

    }

    private void initConnection(String macAddress) {
        brickConnection.setBtAddress(macAddress);

        brickClient = new EV3BrickClient(Layer.L0);
        brickClient.connect(brickConnection);

        brickClient.setDeviceListener(new EV3BrickClient.DeviceListener() {
            @Override
            public void onValueChanged(InputPort inputPort, EV3Sensor ev3Sensor) {
                // 处理反馈回来的数据。
            }

            @Override
            public void onLink(InputPort inputPort, EV3InputDevice ev3InputDevice) {

            }

            @Override
            public void onLink(OutputPort outputPort, EV3OutputDevice ev3OutputDevice) {

            }

            @Override
            public void onLost(InputPort inputPort, EV3InputDevice ev3InputDevice) {

            }

            @Override
            public void onLost(OutputPort outputPort, EV3OutputDevice ev3OutputDevice) {

            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (brickConnection != null) {
            try {
                brickConnection.disconnect();
                brickConnection = null;
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }

}